
package guipk;

import commonpk.DBOperation;
import java.sql.SQLException;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author LJC
 */
public class PlayerFrame extends javax.swing.JFrame {

    //****************************************************************
    /* 成员 */
    private Random          random;                     // 随机数器
    private DBOperation     DBO;                        // 数据库类  
    private ImageIcon       flagImage;                  // 国旗图片
    
    private String          answer = "";                // 用户答案
    private int             currentScore;               // 本次当前得分
    private int             playerId;                   // 玩家账号
    private int             playerScore;                // 玩家积分
    //****************************************************************
    
    
    
    //****************************************************************
    /* 构造器 */
    public PlayerFrame() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.random = new Random();
        this.DBO = new DBOperation();
        this.currentScore = 0;
        this.currentScoreLabel.setText(currentScore + "");
    }
    
    public PlayerFrame(int playerId){
        initComponents();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.random = new Random();
        this.DBO = new DBOperation();
        this.currentScore = 0;
        this.currentScoreLabel.setText(currentScore + "");
        this.playerId = playerId;
    }
    //****************************************************************
    
    
    
    //****************************************************************
    /* 其他方法 */
    // 获取随机国旗序号
    public int getRandomFlagId(){      
        int n;
        n = random.nextInt(getRecordRows()) + 1;
        System.out.println(n + "");
        return n;
    }
    
    // 获取表中记录数
    public int getRecordRows(){
        int n = 0;
        
        try{
            DBO.DBQuary("select * from CountryTable");            
            DBO.getRs().last();        // 将游标移到表尾
            n = DBO.getRs().getRow();
            DBO.getRs().first();       // 将游标移到表头
        }catch(SQLException e){
            e.printStackTrace();
        }
        
        return n;
    }
    //****************************************************************
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        flagLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        answerTextField = new javax.swing.JTextField();
        sendButton = new javax.swing.JButton();
        clearButton = new javax.swing.JButton();
        tipLabel = new javax.swing.JLabel();
        returnButton = new javax.swing.JButton();
        scoreTipLabel = new javax.swing.JLabel();
        currentScoreLabel = new javax.swing.JLabel();
        startButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("猜国旗");
        setBounds(new java.awt.Rectangle(400, 300, 0, 0));
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jPanel1.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        jPanel1.setOpaque(false);
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        flagLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.add(flagLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 20, 273, 182));

        jLabel2.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
        jLabel2.setText("国旗：");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 20, -1, -1));

        jLabel3.setFont(new java.awt.Font("楷体", 1, 20)); // NOI18N
        jLabel3.setText("回答");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, -1, 30));
        jPanel1.add(answerTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 100, 120, 30));

        sendButton.setFont(new java.awt.Font("宋体", 1, 15)); // NOI18N
        sendButton.setText("提交");
        sendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });
        jPanel1.add(sendButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 160, -1, -1));

        clearButton.setFont(new java.awt.Font("宋体", 1, 15)); // NOI18N
        clearButton.setText("清空");
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });
        jPanel1.add(clearButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 160, -1, -1));

        tipLabel.setFont(new java.awt.Font("宋体", 1, 15)); // NOI18N
        jPanel1.add(tipLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 130, 100, 20));

        returnButton.setFont(new java.awt.Font("宋体", 1, 18)); // NOI18N
        returnButton.setText("返回");
        returnButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                returnButtonActionPerformed(evt);
            }
        });
        jPanel1.add(returnButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 220, 80, 40));

        scoreTipLabel.setFont(new java.awt.Font("宋体", 1, 18)); // NOI18N
        scoreTipLabel.setText("本次得分：");
        jPanel1.add(scoreTipLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 240, -1, -1));

        currentScoreLabel.setFont(new java.awt.Font("宋体", 1, 15)); // NOI18N
        jPanel1.add(currentScoreLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 230, 90, 40));

        startButton.setFont(new java.awt.Font("宋体", 1, 15)); // NOI18N
        startButton.setText("开始");
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });
        jPanel1.add(startButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        // TODO add your handling code here:
        this.answerTextField.setText("");
    }//GEN-LAST:event_clearButtonActionPerformed

    private void returnButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_returnButtonActionPerformed
        // TODO add your handling code here:
        
        // 计算积分
        DBO.DBQuary("select * from PlayerTable where PlayerId=" + this.playerId);
        try{
            DBO.getRs().first();
            this.playerScore = DBO.getRs().getInt("PlayerScore");
        }catch(SQLException e){
            e.printStackTrace();
        }
        this.playerScore += this.currentScore;
        
        
        //更新数据库
        String sql = "update PlayerTable set PlayerScore=" + this.playerScore + " where PlayerId=" + this.playerId;
        DBO.DBUpdate(sql);
        
        //弹窗
        new PlayerGoFrame(this.playerId);
        this.dispose();
    }//GEN-LAST:event_returnButtonActionPerformed

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
        // TODO add your handling code here:
        
        // 记录 随机图号
        int flagNum = this.getRandomFlagId();
        System.out.println("Flag Num: " + flagNum);
        // 生成 查询语句
        String sql = "select * from CountryTable where countryId=" + flagNum;
        //执行 查询语句， 获得随机记录
        DBO.DBQuary(sql);
        // 从路径生成图片并显示
        try{
            DBO.getRs().first();
            flagImage = new ImageIcon(DBO.getRs().getString("countryFlagPath"));
        }catch(SQLException e){
            e.printStackTrace();
        }
        this.flagLabel.setIcon(flagImage);
        
    }//GEN-LAST:event_startButtonActionPerformed

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed
        // TODO add your handling code here:
        this.answer = this.answerTextField.getText();
        
        try{
            if(this.answer.equals(this.DBO.getRs().getString("countryChineseBrief"))){
                this.currentScore += 5;
                this.currentScoreLabel.setText(currentScore + "");
                JOptionPane.showMessageDialog(this, "回答正确", "结果", JOptionPane.INFORMATION_MESSAGE);
            }
            else{
                this.currentScore -= 2;
                this.currentScoreLabel.setText(currentScore + "");
                JOptionPane.showMessageDialog(this, "回答错误", "结果", JOptionPane.INFORMATION_MESSAGE);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        
        
        // 以下为开始模块拷贝    ----可封装----
        
        // 记录 随机图号
        int flagNum = this.getRandomFlagId();
        // 生成 查询语句
        String sql = "select * from CountryTable where countryId=" + flagNum;
        //执行 查询语句， 获得随机记录
        DBO.DBQuary(sql);
        // 从路径生成图片并显示
        try{
            DBO.getRs().first();
            flagImage = new ImageIcon(DBO.getRs().getString("countryFlagPath"));
        }catch(SQLException e){
            e.printStackTrace();
        }
        this.flagLabel.setIcon(flagImage);
        
    }//GEN-LAST:event_sendButtonActionPerformed

    
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PlayerFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PlayerFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PlayerFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PlayerFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PlayerFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField answerTextField;
    private javax.swing.JButton clearButton;
    private javax.swing.JLabel currentScoreLabel;
    private javax.swing.JLabel flagLabel;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton returnButton;
    private javax.swing.JLabel scoreTipLabel;
    private javax.swing.JButton sendButton;
    private javax.swing.JButton startButton;
    private javax.swing.JLabel tipLabel;
    // End of variables declaration//GEN-END:variables
}
